package com.example.noactivityresult

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

/**
 *
 *
 * @author: tuyrt7 on 2020/11/23.
 */
class ThirdActivity : AppCompatActivity() {

    val textView by lazy {
        findViewById<TextView>(R.id.textView)
    }

    private val myActivityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
        if (activityResult.resultCode == Activity.RESULT_OK) {
            val result = activityResult.data?.getStringExtra("result")
            Toast.makeText(applicationContext, result, Toast.LENGTH_SHORT).show()
            textView.text = "回传数据：$result"
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_third)

        myActivityLauncher.launch(Intent(this, SecondActivity::class.java).apply {
            putExtra("name", "Hello,技术最TOP3")
        })
    }
}